//
//  ShgztVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/8.
//

import UIKit

class ShgztVC: BaseVC {
    
    var collectionView: UICollectionView!

    var dataArray: [[String: Any]] = []
    var pageNum: Int = 1
    var pageSize: Int = 10
    var keyWord: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadData))
        collectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        NotificationCenter.default.addObserver(self, selector: #selector(ShgztVC.loadData), name: NSNotification.Name(rawValue: "ReloadShgztVC"), object: nil)
        loadData()
    }
    
    // 数据刷新
    @objc func loadData() {
        pageNum = 1
        dataArray.removeAll()
        loadMoreData()
    }
    @objc func loadMoreData() {
        let paramet: [String: Any] = [
            "encryptedData": [
                "auditUser": EWEUser.lgf.userId ?? "",
                "hospitalId": EWEUser.lgf.hospitalId ?? "",
                "roleIds":[
                ],
                "keyWord" : keyWord,
                "pageNo": pageNum.description,
                "pageSize": pageSize.description
            ]
        ]
        EWERequest.lgf("audit", paramet, .get) { [weak self] (data) in
            let result = (data["result"] as? [[String: Any]]) ?? []
            if result.count > 0 {
                self?.dataArray += result
                self?.pageNum += 1
            }
            self?.collectionView.reloadData()
            self?.endRefreshing()
            if result.count == 0 {
                self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
            }
        } _: { [weak self] (error) in
            SVProgressHUD.showError(error)
            self?.endRefreshing()
        }
    }
    func endRefreshing() {
        self.collectionView.mj_header?.endRefreshing()
        self.collectionView.mj_footer?.endRefreshing()
    }

}

extension ShgztVC: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        keyWord = textField.text ?? ""
        loadData()
    }
}

extension ShgztVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: UIDevice.lgf_ScreenW, height: 100)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: BzgztCell.self, for: indexPath)
        cell.tzzx.text = (dataArray[indexPath.item]["name"] as? String) ?? ""
        cell.sj.text = "样本数量：" + ((dataArray[indexPath.item]["sampleTotal"] as? Int) ?? 0).description
        let status = (dataArray[indexPath.item]["status"] as? Int) ?? 10
        if status == 10 {
            cell.status_l.text = "审核开始"
            cell.status_l.textColor = UIColor.init(lgf_HexString: "8D969D")
            cell.status_i.image = #imageLiteral(resourceName: "shenhekaishi@1.5x")
        } else if status == 20 {
            cell.status_l.text = "审核完成"
            cell.status_l.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.status_i.image = #imageLiteral(resourceName: "shenhewancheng@1.5x")
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let status = (dataArray[indexPath.item]["status"] as? Int) ?? 10
        let vc = TzbzshVC()
        vc.type = "审核"
        vc.status = status
        vc.id = (dataArray[indexPath.item]["id"] as? String) ?? ""
        vc.navi_title = (dataArray[indexPath.item]["name"] as? String) ?? ""
        self.navigationController?.show(vc, sender: self)
    }
}
